﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using ZOVNP.Model.Abstracts;

namespace ZOVNP.WinView
{
    static class Program
    {
        /// <summary> The main entry point for the application. </summary>
        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;

                // создаем настройки и сервисы
                //using (ZOVNPSettingsController sc = new SkifBPSettingsController())
                using (Login login = new Login())
                {
                    new CommandLineParser(args, login);
                    Application.Run(new MainForm(new SQLEngineProvider(login)));
                }
                // при sc.Dispose() происходит сохранение настроек
            }
            catch (Exception exception)
            {
                new ServiceLocatorHelper().ServiceLocator.GetService<IMessageService>().ShowError(exception);
            }
            new ServiceLocatorHelper().ServiceLocator.Dispose();
        }
        /// <remarks> Возможно, мы об этом пожалеем... </remarks>
        static Assembly OnAssemblyResolve(object sender, ResolveEventArgs args)
        {
            foreach (Assembly assembly in ((AppDomain)sender).GetAssemblies())
            {
                if (assembly.FullName == args.Name)
                    return (assembly);
            }
            return (null);
        }
    }
}
